export default {
  namespaced: true,
  state: {
    // query: {},
    list: [], //存放所有商品对象
    prodesc: {}, //存放一个商品对象
    car: [], //放购物车的所有信息
    num: '',
    cartid: '',
    checkedgoods: [], //选中的商品列表
    orderuser: '', //订购人姓名
    orderphone: '', //订购人电话
    ordermessage: '', //留言
    lastaddress: {
      receiver: '',
      mobile: '',
      regions: '',
      address: '',
    },
    keyword: '',
    allorder: [],
  },
  mutations: {
    save(state, payload) {
      state.list = payload.list;
    },
    savecarts(state, payload) {
      state.car = payload.carts;
    },
    saveDetail(state, payload) {
      state.prodesc = payload.data;
    },
    saveCartId(state, payload) {
      state.cartid = payload.cartid;
    },
    saveChecked(state, payload) {
      state.checkedgoods = payload.checkedgoods;
    },
    saveOrder(state, payload) {
      state.orderuser = payload.orderuser;
      state.orderphone = payload.orderphone;
    },
    saveMessage(state, payload) {
      state.ordermessage = payload.ordermessage;
    },
    saveAddress(state, payload) {
      state.lastaddress = payload;
    },
    saveKeyword(state, payload) {
      state.keyword = payload.keyword;
    },
    // saveStatus(state, payload) {
    //   state.status = payload.status;
    // },
    saveAllOrder(state, payload) {
      state.allorder.push(payload.currentorder); //将新订单（是一个对象）加入数组
    },
  },
  getters: {
    //相当于计算属性
    getAllCount(state) {
      var c = 0;
      state.car.forEach(item => {
        c += item.quantity;
      });
      console.log('getters中方法执行了');
      console.log(c);
      return c;
    },
  },
  // 传的第一个参数固定是state对象，第二个参数是组件传过来的
  actions: {
    loadData({ commit }, payload) {
      commit('save', payload);
    },
  },
};
